package cn.hw.server.controller;


import cn.hw.server.service.UserService;
import cn.hw.server.util.ReturnBean;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Api(value = "用户接口", tags = "系统用户相关接口，用户添加，修改，删除，重置密码")
@RestController
@CrossOrigin
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("add")
    public ReturnBean add(
            @RequestParam String nickName,
            @RequestParam String loginName,
            @RequestParam String loginPassword,
            @RequestParam String address,
            @RequestParam String tel,
            @RequestParam String sex) {

        userService.add(nickName, loginName, loginPassword, address, tel, sex);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }

    @PostMapping("update")
    public ReturnBean update(
            @RequestParam Integer id,
            @RequestParam String nickName,
            @RequestParam String address,
            @RequestParam String tel,
            @RequestParam String sex,
            @RequestParam Integer rId) {
        boolean update = userService.update(id, nickName, address, tel, sex);
        if (update) {
            return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
        }
        return ReturnBean.of(ReturnBean.AnswerCode.OBJECT_NOT_EXIST);
    }


    @PostMapping("resetPwd")
    public ReturnBean resetPwd(
            @RequestParam Integer id) {
        boolean b = userService.resetPwd(id);
        if (b) {
            return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, "密码成功重置为：123456 ，请牢记");
        }
        return ReturnBean.of(ReturnBean.AnswerCode.OBJECT_NOT_EXIST);
    }

    @PostMapping("list")
    public ReturnBean list(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "15") Integer size) {
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, userService.list(page, size));
    }

    @PostMapping("findById")
    public ReturnBean findById(
            @RequestParam Integer id) {
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, userService.findById(id));
    }

    @PostMapping("delete")
    public ReturnBean delete(
            @RequestParam Integer id) {
        userService.delete(id);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }


}
